package ShareLife.gar;

import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;

public class ContactsLayer {
	public int IDx[] = { 40, 180, 40, 180, 40, 180, 40 };
	public int IDy[] = { 30, 30, 140, 140, 250, 250, 360 };
	public int endX[] = { -100, 320, -100, 320, -100, 320, -100 };
	public int endY[] = { 10, 10, 120, 120, 270, 270, 380 };
	private Rect[] containerEvent;
	public Vector<PlayerInfo> contactslist = null;
	public int layerindex;
	public int beginindex;
	public boolean animationRunning;
	public boolean isFocus;
	public String layername = null;
	private int layerx;
	public boolean uptodown;

	public ContactsLayer() {
		contactslist = new Vector<PlayerInfo>();
		uptodown = true;
		initalEventArea();
	}

	public void setX(int aX) {
		layerx = aX;

	}

	public int getX() {
		return layerx;

	}

	public void addContact(PlayerInfo aPlayerInfo) {
		contactslist.addElement(aPlayerInfo);
	}

	public void modifiedVisble(boolean aUp) {
		int tempindex = 0;
		int tempbegin = 0;
		int tempend = 0;
		if (aUp) {
			tempend = tempindex = beginindex + 7 > contactslist.size() ? contactslist
					.size() : beginindex + 7;
			tempbegin = beginindex;
		} else {
			tempbegin = tempindex = beginindex - 14 < 0 ? 0 : beginindex - 14;
			tempend = tempindex + 7;
		}
		for (int i = tempbegin; i < tempend; i++) {
			PlayerInfo tempPlayer = contactslist.elementAt(i);
			tempPlayer.visbile = true;
		}
	}

	public void modifedfinished(boolean aUp) {
		int tempindex = 0;
		int tempcontactnumber = contactslist.size();
//		int tempnumber = (tempcontactnumber - (tempcontactnumber / 7 * 7));
		if (aUp) {
			tempindex = beginindex - 7 < 0 ? 0 : beginindex - 7;
		}
		else
		{
			tempindex = ((beginindex-1)/7)*7;
		}

		for (int j = tempindex; j < beginindex; j++) {
			PlayerInfo tempPlayer = contactslist.elementAt(j);
			tempPlayer.visbile = false;

		}
		if (aUp) {
			tempindex = beginindex + 7 > contactslist.size() ? contactslist
					.size() : beginindex + 7;
		}
		beginindex = tempindex;
		animationRunning = false;
		
	}

	public void initalLocation() {

		int tempindex = contactslist.size() > 7 ? 7 : contactslist.size();
		for (int i = 0; i < tempindex; i++) {
			PlayerInfo tempPlayer = contactslist.elementAt(i);
			tempPlayer.visbile = true;
			tempPlayer.setLocation(IDx[i % 7], IDy[i % 7]);
		}
		for (int i = tempindex; i < contactslist.size(); i++) {
			PlayerInfo tempPlayer = contactslist.elementAt(i);
			tempPlayer.visbile = false;
			tempPlayer.setLocation(300, 500);
		}
		beginindex = tempindex;
	}

	public void drawLayer(Canvas aGraphics, Paint aPaint) {
		aGraphics.translate(layerx, 0);
		for (int i = 0; i < contactslist.size(); i++) {
			PlayerInfo tempPlayer = contactslist.elementAt(i);
			tempPlayer.drawSelf(aGraphics, aPaint);
		}
		aGraphics.translate(-layerx, 0);
	}

	public void initalEventArea() {
		containerEvent = new Rect[7];
		for (int i = 0; i < 7; i++) {
			containerEvent[i] = new Rect(IDx[i], IDy[i], IDx[i] + 100,
					IDy[i] + 100);
		}
	}

	public int GetTouchEventType(MotionEvent e) {
		int locationX = (int) e.getX();
		int locationY = (int) e.getY();

		int tempBegin = (beginindex-1)/7*7;
		
		for (int i = tempBegin; i < beginindex; i++) {
			if (containerEvent[i-tempBegin].contains(locationX, locationY)) {
				Log.i("Touchevent.........", "Touchevent..............."
						+ beginindex + "   " + i);
				return (i);
			}
		}
		return -1;
	}

	public PlayerInfo handleEvent(int aIndex) {
		return contactslist.elementAt(aIndex);
	}
}
